<template>
  <el-drawer :visible.sync="visible" :title="title" :modal-append-to-body="true" :append-to-body="true"
    :destroy-on-close="false" :modal="true" direction="rtl" :wrapperClosable="false" custom-class="form-drawer"
    size="450px" @closed="closed">
    <div class="form-drawer__content">
      <el-form :inline="true" class="demo-form-inline" style="flex:none;padding-top: 0px;
    padding-bottom: 5px;
    padding-left: 5px;">
        <el-form-item style="margin-bottom:0px;">
          <el-select v-model="selectId" clearable placeholder="请选择场景" @change="handleSelectChange">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" border empty-text="暂无数据." @row-click="rowClickHandle" highlight-current-row
        style="width: 100%">
        <el-table-column align="center" width="35" label="">
          <template slot-scope="scope">
            <el-radio class="radio" v-model="rowSelection" :label="scope.row.id"></el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="图层样式" width="120px" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 110px" fit="fit" :src="scope.row.url">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="图层名称">
        </el-table-column>
      </el-table>
      <div style="text-align: right;padding-top: 0.3125rem;padding-bottom: 0.3125rem;">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.cur"
          :page-size="page.limit" :total="page.total" :page-sizes="[20, 30, 50, 100]"
          layout="prev, pager, next, total, sizes">
        </el-pagination>
      </div>
      <div class="form-drawer__footer">
        <el-button @click="closed" :loading="loading">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      </div>
    </div>

  </el-drawer>
</template>

<script>
  import {
    findLayerList,
    getOptions,
  } from '@/api/subsystem/layer'
  import {
    saveElementLayer
  } from '@/api/subsystem/poi'
  import {
    isOk
  } from '@/utils/request'
  export default {
    name: 'LayerDialog',
    components: {},
    props: {
      properties: {
        type: Object,
        default: () => ({})
      }
    },
    computed: {},
    watch: {
      properties: {
        handler: function(value) {
          this.visible = value.visible
          let ids = [];
          if (value.data) {
            value.data.forEach(function(item, index) {
              ids.push(item.id)
            })
            this.elementIds = ids
          }
        },
        immediate: true,
        deep: false
      }
    },
    data() {
      return {
        visible: false,
        title: '场景图层',
        baseUrl: process.env.VUE_APP_BASE_API,
        loading: false,
        tableData: [],
        options: [],
        selectId: '',
        sceneId: '',
        rowSelection: '',
        radioCheckList: [],
        elementIds: [],
        page: {
          cur: 1,
          limit: 50,
          total: 0
        },
      }
    },
    created() {
      this.load()
      this.getOptions()
    },
    methods: {
      load() {
        this.loading = true
        findLayerList({
            sceneId: this.sceneId,
            cur: this.page.cur,
            limit: this.page.limit,
          }).then(rsp => {
            if (isOk(rsp)) {
              const _data = rsp.result
              this.page.total = _data.totalRow
              _data.list.forEach(item => {
                item['url'] = this.baseUrl + '/biz/scene/thumbnail/' + item.id + '_normal'
              })
              this.tableData = _data.list
              this.loading = false
            }
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
      },
      handleSizeChange(val) {
        this.page.limit = val
        this.load()
      },
      handleCurrentChange(cur) {
        this.page.cur = cur
        this.load()
      },
      handleSelectChange(e) {
        this.sceneId = e
        this.load()
      },
      getOptions: function() {
        getOptions().then(rsp => {
          if (200 == rsp.code) {
            this.options = rsp.result
          }
        })
      },
      rowClickHandle(row) {
        this.rowSelection = row.id
        this.radioCheckList = this.tableData.filter((item) => item.id === row.id)
      },
      submitForm: function() {
        if (this.radioCheckList.length == 0) {
          this.$message.warning('请选择图层！');
          return;
        }
        this.loading = true
        saveElementLayer({
            elementIds: JSON.stringify(this.elementIds),
            layerId: this.radioCheckList[0].id
          }).then(rsp => {
            if (200 == rsp.code) {
              this.$message({
                type: 'success',
                message: rsp.message
              })
            }
            this.closed()
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
      },
      closed: function() {
        this.$emit('closed', false)
      },
    }
  }
</script>


<style scoped>
  /deep/.form-drawer__footer button {
    flex: 1;
  }

  /deep/.el-drawer__body {
    overflow-y: auto;
  }

  /deep/.form-drawer__content {
    overflow-y: auto;
  }

  /deep/.el-table {
    overflow-y: auto;
  }
</style>
